package com.triangle.cashier.net;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * rest 请求工厂
 * Created by  on 2018/6/19.
 */

public class RetrofitFactory {

    public static final String BASE_URL = "http://apis.baidu.com/apistore/weatherservice/";
    private static RetrofitFactory instance;
    private Retrofit retrofit;


    public RetrofitFactory() {
        OkHttpClient client = new OkHttpClient()
                .newBuilder()
                .connectTimeout(8, TimeUnit.SECONDS)
                .readTimeout(8, TimeUnit.SECONDS)
                .writeTimeout(8, TimeUnit.SECONDS)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }

    public static RetrofitFactory getInstance() {
        if (instance == null) {
            synchronized (RetrofitFactory.class) {
                if (instance == null) {
                    instance = new RetrofitFactory();
                }
            }
        }
        return instance;
    }


    public RetrofitService getService() {
        RetrofitService service = retrofit.create(RetrofitService.class);
        return service;
    }


}
